<script setup lang="ts">
import { ref, PropType, onMounted } from 'vue'

interface User {
  name: string
  age: string
}

const props = defineProps({
  msg: {
    type: String,
    default: () => '',
  },
  user: {
    type: Object as PropType<User>,
    default: () => {
      return {
        name: 'rays77',
        age: '18',
      }
    },
  },
})

// const props1 = withDefaults(defineProps<User>(), {
//   name: 'rays77',
//   age: '18'
// })

// defineEmits：子组件调用父组件中的方法
const emit = defineEmits(['increment'])

onMounted(() => {
  console.log('获取h1 DOM元素', title.value)
})

const increment = () => {
  console.log(props.msg)
  count.value++
  emit('increment')
}

const count = ref(0)
const title = ref<HTMLHeadElement | null>(null) // 定义获取dom元素
const foo = ref<{
  a: number
  b: string
} | null>(null)

foo.value = {
  a: 1,
  b: 'hi',
}
</script>

<template>
  <h1 ref="title">{{ foo?.b }} - {{ msg }}</h1>

  <button @click="increment">count : +1</button>

  <p>
    Recommended IDE setup:
    <a href="https://code.visualstudio.com/" target="_blank">VSCode</a>
    +
    <a href="https://github.com/johnsoncodehk/volar" target="_blank">Volar</a>
  </p>

  <p>See <code>README.md</code> for more information.</p>

  <p>
    <a href="https://vitejs.dev/guide/features.html" target="_blank">
      Vite Docs
    </a>
    |
    <a href="https://v3.vuejs.org/" target="_blank">Vue 3 Docs</a>
  </p>

  <button type="button" @click="count++">count is: {{ count }}</button>
  <p>
    Edit
    <code>components/HelloWorld.vue</code> to test hot module replacement.
  </p>
</template>

<style scoped>
a {
  color: #42b983;
}

label {
  margin: 0 0.5em;
  font-weight: bold;
}

code {
  background-color: #eee;
  padding: 2px 4px;
  border-radius: 4px;
  color: #304455;
}
</style>
